import {Component, EventEmitter, Input, OnInit, Output, SimpleChanges} from '@angular/core';
import {BaseService} from "../../../shared/service/base.service";
import {ApiUrl} from "../../../config/api-url";

@Component({
  selector: 'app-device-set-pulse',
  templateUrl: './device-set-pulse.component.html',
  styleUrls: ['./device-set-pulse.component.less']
})
export class DeviceSetPulseComponent implements OnInit {

  @Input()
  isVisible = false;

  @Output()
  emitVisible: EventEmitter<any> = new EventEmitter();
  isOkLoading: any;
  isSpinning: any;

  @Input()
  inputDevice: any;

  heartbeatTime: any;
  coinPulseId: any;
  ritualModel: any;

  // 间隔长度
  gapLength: number;
  // 脉冲长度
  pulseLength: number;
  //出礼脉冲间隔
  outgoingGapLength: number;
  //出礼脉冲长度
  outgoingPulseLength: number;

  // 脉冲间隔、宽度集合
  coinPulseList: any

  constructor(private baseService: BaseService) { }

  ngOnInit(): void {
    this.coinPulseList = new Array();
  }

  handleCancel() {
    this.isSpinning = false;
    this.isVisible = false;
    this.emitVisible.emit(this.isVisible);
  }

  findOneDeviceById(){
    this.isSpinning = true;
    if (this.inputDevice.id == null){
      this.isSpinning = false;
      this.baseService.layer.messageError('设备ID不能为空');
      return;
    }

    this.baseService.get(ApiUrl.DeviceUrl + '/' + this.inputDevice.id).subscribe(
        data => {
          this.isSpinning = false;
          if (data.status == 0) {
            this.inputDevice = data.data
            this.initData();
          } else {
            this.baseService.layer.messageError('查询不成功：' + data.msg);
          }
        }
    );
  }

  handleOk() {
    this.isSpinning = true;
    if (this.heartbeatTime == null){
      this.isSpinning = false;
      this.baseService.layer.messageError('请选择心跳时间');
      return;
    }
    if (this.gapLength == null){
      this.isSpinning = false;
      this.baseService.layer.messageError('请输入投币脉冲间隔长度');
      return;
    }
    if (this.pulseLength == null){
      this.isSpinning = false;
      this.baseService.layer.messageError('请输入投币脉冲长度');
      return;
    }
    if (this.ritualModel == null){
      this.isSpinning = false;
      this.baseService.layer.messageError('请选择出礼模式');
      return;
    }
    if (this.outgoingGapLength == null || this.outgoingPulseLength == null){
      this.isSpinning = false;
      this.baseService.layer.messageError('请选择出礼脉冲间隔、宽度');
      return;
    }

    let body = {
      id: this.inputDevice.id,
      heartbeatTime: this.heartbeatTime,
      gapLength:this.gapLength,
      pulseLength:this.pulseLength,
      ritualModel:this.ritualModel,
      outgoingGapLength:this.outgoingGapLength,
      outgoingPulseLength:this.outgoingPulseLength
    };

    // console.info(body)
    this.baseService.put(ApiUrl.DeviceUrl + '/sendUpdateBoxConfigPulse', body).subscribe(
      data => {
        this.isSpinning = false;
        if (data.status == 0) {
          this.isVisible = false;
          this.emitVisible.emit(this.isVisible);
          this.baseService.layer.messageSuccess('发送成功！');
        } else {
          this.baseService.layer.messageError('发送失败：' + data.msg);
        }
      }
    );
  }

  ngOnChanges(changes: SimpleChanges): void {
    if(this.isVisible) {
      try {
        this.findOneDeviceById();
      } catch (e) {
        console.log(e.msg);
      }
    }
  }

  initData(){
    this.heartbeatTime = this.inputDevice.heartbeatTime + "";
    this.coinPulseId = this.inputDevice.coinPulseId;
    this.ritualModel = this.inputDevice.ritualModel + "";
    this.gapLength = this.inputDevice.gapLength;
    this.pulseLength = this.inputDevice.pulseLength;
    this.outgoingGapLength = this.inputDevice.outgoingGapLength;
    this.outgoingPulseLength = this.inputDevice.outgoingPulseLength;
  }
}
